package com.persondemo.videoappliction.ui.duanzi;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.persondemo.videoappliction.R;
import com.persondemo.videoappliction.bean.ZiXunBean;
import com.persondemo.videoappliction.component.ApplicationComponent;
import com.persondemo.videoappliction.component.DaggerHttpComponent;

import com.persondemo.videoappliction.ui.adapter.ZiXunAdapter;
import com.persondemo.videoappliction.ui.base.BaseFragment;
import com.persondemo.videoappliction.ui.duanzi.contract.ZiXunContract;
import com.persondemo.videoappliction.ui.duanzi.presenter.ZiXunPresenter;
import com.persondemo.videoappliction.ui.search.SeachWebActivity;
import com.persondemo.videoappliction.ui.video.WeBviewActivity;
import com.persondemo.videoappliction.utils.T;
import com.persondemo.videoappliction.widget.CustomLoadMoreView;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

public class ZiXunFragment extends BaseFragment<ZiXunPresenter> implements ZiXunContract.View {
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.SimpleMultiStateView)
    com.persondemo.videoappliction.widget.SimpleMultiStateView SimpleMultiStateView;
    @BindView(R.id.mPtrFrameLayout)
    PtrClassicFrameLayout mPtrFrameLayout;
    Unbinder unbinder;
    private int page = 1;
    private ZiXunAdapter adapter;
    private List<ZiXunBean.DataBean> list;

    public static ZiXunFragment newInstance() {
        Bundle args = new Bundle();
        ZiXunFragment fragment = new ZiXunFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getContentLayout() {
        return R.layout.module_fragment_zixun;
    }

    @Override
    public void initInjector(ApplicationComponent appComponent) {
        DaggerHttpComponent.builder()
                .applicationComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void bindView(View view, Bundle savedInstanceState) {
        mPtrFrameLayout.disableWhenHorizontalMove(true);
        mPtrFrameLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, mRecyclerView, header);
            }
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                if (mPresenter != null) {
                    page = 1;
                    showLoadingDialog();
                    mPresenter.getzixun(page + "");
                }
            }
        });
        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        adapter = new ZiXunAdapter(list, getActivity());
        mRecyclerView.setAdapter(adapter);
        adapter.setEnableLoadMore(true);
        adapter.setLoadMoreView(new CustomLoadMoreView());
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MobclickAgent.onEvent(getActivity(), "xingwen");
                Intent intent = new Intent(getActivity(), XingWenWebActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("title", list.get(position).getHref());
                bundle.putString("text", list.get(position).getTitle());
                intent.putExtras(bundle);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.slide_in_left, R.anim.slide_in_right);
            }
        });

        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.getzixun(page + "");
            }
        }, mRecyclerView);
    }

    @Override
    public void initData() {
        list = new ArrayList<>();
        if (mPresenter != null) {
            mPresenter.getzixun(page + "");
        }
    }

    @Override
    public void loadzixun(ZiXunBean ziXunBean) {
     hideLoadingDialog();
        if (ziXunBean.getStatus()==1){
        if (ziXunBean.getData()!=null&&ziXunBean.getData().size()>0){
            list.clear();
            list=ziXunBean.getData();
            adapter.setNewData(list);
            mPtrFrameLayout.refreshComplete();
            showSuccess();
            page++;
            hideLoadingDialog();
            return;
        }
        if (ziXunBean.getData()==null||ziXunBean.getData().size() == 0){
            T.showShort(getContext(), "没有更多数据了！");
            return;
        }
        }else {
            T.showShort(getActivity(),ziXunBean.getMsg());
        }
    }

    @Override
    public void loadmore(ZiXunBean ziXunBean) {
        if (ziXunBean.getData() == null || ziXunBean.getData().size() == 0) {
            adapter.loadMoreEnd();
            T.showLong(getActivity(), "没有更多数据了！");
            return;
        } else {
            page++;
            adapter.addData(ziXunBean.getData());
            adapter.loadMoreComplete();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
